<?php
namespace app\common\model;

use app\common\traits\Gate;
use think\Model;
use think\facade\Session;

class Users extends Model
{
    use Gate;
    protected $updateTime = 'last_login_time';
    protected $createTime = 'register_time';
    protected $update  = ['last_login_ip'];
    protected $autoWriteTimestamp = 'int';
    protected $type = [
        'last_login_time'  =>  'timestamp',
        'register_time'  =>  'timestamp',
    ];
    /**
     * 返回用户的用户名
     * @param $uid
     * @return mixed
     */
    public function getNickName($uid){
        return $this->where(array('id'=>(int)$uid))->value('username');
    }

    /**
     * 用户状态修改器
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStatusTextAttr($value, $data)
    {
        $status = [1=>'<span class="label label-success">正常</span>',2=>'<span class="label label-danger">禁用</span>'];
        return $status[$data['status']];
    }
    public function getWeixinstatusTextAttr($value,$data)
    {
        $status = [1=>'<span class="label label-success">已绑定</span>',2=>'<span  class="label label-danger">未绑定</span>'];
        return $status[$data['weixinstatus']];
    }
    public function edit(){
        halt($this->checkRule());
    }
    /**
     * 返回ip
     * @return mixed
     */
    protected function setLastLoginIpAttr()
    {
        return request()->ip();
    }
    //获取用户角色
    public function juese(){
        return $this->belongsToMany('AuthGroup', 'auth_group_access','group_id','uid');
    }
    //获取用户部门
    public function depar(){
        return $this->belongsTo('depar','depar_id','id',[],"LEFT");
    }
    //获取用户地区
    public function dq(){
        return $this->belongsTo('dq','dq_id','id',[],"LEFT");
    }
    //获取用户组
    public function group(){
        return $this->belongsTo('group','group_id','id',[],"LEFT");
    }
    //获取邀约分组
    public function ygroup(){
        return $this->belongsTo('ygroup','ygroup_id','id',[],"LEFT");
    }
    /**
     * 用户登录
     * @param string $username 用户名
     * @param string $password 密码
     * @param bool $rememberme 记住登录
     * @return bool|mixed
     */
    public function login($username = '', $password = '', $rememberme = false)
    {
        $username = trim($username);
        $password = trim($password);
        $map['username']=$username;
        $map['status']=1;
        $user = self::get($map);
        if (!$user) {
            $this->error = '用户不存在或被禁用！';
        } else {
            if (md5($password)!==$user['password']) {
                $this->error = '用户名或者密码错误！';
            } elseif($user['weixinstatus']!=1 && $user['id']!=1){
                $this->error = '请绑定微信公众号之后在登陆系统！';
            }else {
                $id = $user['id'];
                // 更新登录信息
                $user['counts'] = $user['counts']+1;
                if ($user->save()) {
                    // 自动登录
                    $this->auto_login($this->with('depar')->where(['id'=>$id])->find(), $rememberme);
                }
                return $id;

            }
        }
        return false;
    }
    /**
     * 微信用户登录
     * @param array $users 用户名
     * @param bool $rememberme 记住登录
     * @return bool|mixed
     */
    public function wechatlogin($users, $rememberme = false)
    {
        $map['openid']=$users['id'];
        $user = self::get($map);
        $id = $user['id'];
        // 更新登录信息
        $user['counts'] = $user['counts']+1;
        if ($user->save()) {
            // 自动登录
            $this->auto_login(self::get($id), $rememberme);
            return $id;
        }else{
            return false;
        }

    }
    /**
     * 自动登录
     * @param mixed $user 用户对象
     * @param bool $rememberme 是否记住登录，默认7天
     */
    public function auto_login($user, $rememberme = false)
    {
        // 记录登录
        $auth = array(
            'adminid'  => $user->id,
            'username' => $user->username,
            'depar_id' => $user->depar_id,
            'depar_name' => !empty($user['depar']['name'])?$user['depar']['name']:'',
            'dq_id'    => $user->dq_id,
            'group_id' => $user->group_id,
            'avr'      => $user->headimgurl
        );
        session('admin_user', $auth);
        // 记住登录
        if ($rememberme) {
            $signin_token = $user->username.$user->id;
            cookie('aid', $user->id, 24 * 3600 * 7);
            cookie('signin_token', ($signin_token), 24 * 3600 * 7);
        }
    }
}